home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex11-2.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  2KB  |  87 lines

  1. // ex11-2.c -- Managing N resources with a Semaphore
  2.  
  3. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex11-2.c,v 3.0 90/05/15 22:44:23 kgorlen Rel $
  4.  
  5. #define BASE StackProc
  6. #if BASE == StackProc
  7. #include "StackProc.h"
  8. #endif
  9. #if BASE == HeapProc
  10. #include "HeapProc.h"
  11. #endif
  12.  
  13. #include "OrderedCltn.h"
  14. #include "Scheduler.h"
  15. #include "Semaphore.h"
  16. #include "String.h"
  17.  
  18. class TestProcess : public BASE {
  19. public:
  20.     TestProcess(Semaphore*,OrderedCltn*,stackTy*);
  21.     static TestProcess* create(Semaphore*,OrderedCltn*);
  22. };
  23.  
  24. TestProcess::TestProcess(Semaphore* resourceAvailable,
  25.                          OrderedCltn* resourceQ,
  26.                          stackTy* bot)
  27.             : BASE("TestProcess",bot,1)
  28. {
  29.     // parent process yields to allow this process to start
  30.     if ( FORK() ) { Scheduler::yield(); return; }
  31.  
  32.     // obtain 2 resources
  33.     Object* resource[2];
  34.     int i;
  35.     for (i=0; i<2; i++) {
  36.         resourceAvailable->wait();
  37.         resource[i] = resourceQ->remove(*resourceQ->first());
  38.         cout << name() << ": obtained " 
  39.              << *resource[i] << endl;
  40.         }
  41.  
  42.     // release 2 resources
  43.     for (i=0; i<2; i++) {
  44.         resourceQ->addLast(*resource[i]);
  45.         resourceAvailable->signal();
  46.         cout << name() << ": released " 
  47.              << *resource[i] << endl;
  48.         }
  49.  
  50.     // terminate to avoid return
  51.     terminate();
  52. }
  53.  
  54. TestProcess* TestProcess::create(Semaphore* sem, OrderedCltn* cltn)
  55. {
  56.     // the next two statements must be in the same scope
  57.     // for the address of the stack bottom to be correct
  58.     auto stackTy bottom;
  59.     return new TestProcess(sem,cltn,&bottom);
  60. }
  61.  
  62. main()
  63. {
  64.     // start Scheduler
  65.     // create main context with priority 0
  66.     MAIN_PROCESS(0);
  67.  
  68.     // manage a resource queue of size = N
  69.     const int N = 2;
  70.     OrderedCltn* resourceQ = new OrderedCltn(N);
  71.     Semaphore* resourceAvailable = new Semaphore(N);
  72.     resourceQ->addLast(*new String("resource1"));
  73.     resourceQ->addLast(*new String("resource2"));
  74.     cout << "main: " << resourceQ->size() 
  75.          << " resources available" << endl;
  76.  
  77.     // construct process to use resources
  78.     TestProcess::create(resourceAvailable,resourceQ);
  79.  
  80.     do { // wait for resource to be returned
  81.         resourceAvailable->wait();
  82.         resourceAvailable->signal();
  83.         cout << "main: " << resourceQ->size() 
  84.              << " resources available" << endl;
  85.        } while (resourceQ->size()<N);
  86. }
  87.